import {login, getInfo, logout, loginSms} from '@/api/user'
import {setToken, removeToken} from '@/utils/auth'
import {resetRouter} from '@/router'
import md5 from 'js-md5';

const state = {
    token: '',
    name: '',
    avatar: '',
    userId: ''
}

const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    },
    SET_USERID: (state, userId) => {
        state.userId = userId
    }
}

const actions = {
    // user login
    // 存入token到vuex
    UserLogin({commit}, userInfo) {
        const {username, password} = userInfo
        return new Promise((resolve, reject) => {
            login.post({username: username.trim(), password: md5(password)}).then(response => {
                const data = response;
                if (data.success) {
                    window.localStorage.setItem('userinfo', JSON.stringify(data.data))
                    commit('SET_TOKEN','user'+ data.data.id +'logined');
                    setToken('user'+ data.data.id +'logined')//用户id已登陆
                }
                resolve(data);
            }).catch(error => {
                reject(error)
            })
        })
    },

    UserSmsLogin({commit}, info) {
        const {mobile, smsCode} = info
        return new Promise((resolve, reject) => {
            loginSms.post({mobile: mobile.trim(), smsCode: smsCode.trim()}).then(response => {
                const data = response;
                if (data.code === 1000) {
                    commit('SET_TOKEN', data.data)//存在vueX中
                    setToken(data.data)//存在cookie中
                }
                resolve(data);
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get user info
    // 把获取到的token进行鉴别
    UserGetInfo({commit}) {
        return new Promise((resolve, reject) => {
            getInfo.get().then(response => {
                const {data} = response
                const {name, avatar, userId} = data

                commit('SET_NAME', name)
                commit('SET_AVATAR', avatar)
                commit('SET_USERID', userId)
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    // user logout
    UserLogout({commit, state}) {
        return new Promise((resolve, reject) => {
            logout.post({token: state.token}).then(() => {
                commit('SET_TOKEN', '')
                removeToken()
                resetRouter()
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
    // remove token
    UserResetToken({commit}) {
        return new Promise(resolve => {
            commit('SET_TOKEN', '')
            removeToken()
            resolve()
        })
    }
}
export default {
    state,
    mutations,
    actions
}
